package com.example.pceb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pceb.entity.OperationLog;
import com.example.pceb.result.Result;
import com.example.pceb.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/operation-logs")
public class OperationLogController {
    @Autowired
    private OperationLogService operationLogService;

    // 分页查询
    @GetMapping("/page")
    public Result<IPage<OperationLog>> page(@RequestParam(defaultValue = "1") Integer current,
                                            @RequestParam(defaultValue = "20") Integer size,
                                            @RequestParam(required = false) String username,
                                            @RequestParam(required = false) String operationType) {
        Page<OperationLog> page = new Page<>(current, size);
        IPage<OperationLog> result = operationLogService.pageQuery(page, username, operationType);
        return Result.success(result);
    }

    // 统计
    @GetMapping("/stats")
    public Result<Map<String, Object>> stats() {
        return Result.success(operationLogService.getStats());
    }

    // 清空日志
    @DeleteMapping("/clear")
    public Result<Void> clear() {
        operationLogService.clearAll();
        return Result.success();
    }
} 